﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace Analysis.Controls
{
    public partial class ExcelImportErrorList : System.Web.UI.UserControl
    {
        public string JsCodeToRunOnReady { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadData(DataTable dataSource)
        {
            gridExcelImportError.DataSource = dataSource;
            gridExcelImportError.DataBind();
            upExcelImportError.Update();
        }

        public void ShowDialog()
        {
            JsCodeToRunOnReady = "showExcelImportErrorList();";
        }

        protected void upExcelImportError_Load(object sender, EventArgs e)
        {
        }

        protected void gridExcelImportError_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void btnExcelExport_Click(object sender, EventArgs e)
        {
            ExcelPackage pck = new ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("Лист 1");

            int top = 1;
            int rowStartAutofit = 1;
            int left = 0;

            Button button = sender as Button;
            GridView gv = new GridView();
            if(button != null)
                gv = (GridView)button.Parent.FindControl("gridExcelImportError");
            // Выводим заголовки колонок и запоминаем названия полей бд (в таком же порядке)
            for (int i = 0; i < gv.Columns.Count; ++i)
            {
                ++left;
                ws.Cells[top, left].Value = gv.Columns[i].HeaderText;
            }

            // Стиль заголовка
            var range = ws.Cells[top, 1, top, left];
            range.Style.Font.Bold = true;
            var border = range.Style.Border;
            border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
            range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            ++top;
            left = 1;

            // Выводим данные

            for (int i = 0; i < gv.Rows.Count; ++i)
            {
                for (int j = 0; j < gv.Columns.Count; ++j)
                {
                    if (gv.Rows[i].Cells[j].Text != null)
                        ws.Cells[top + i, left + j].Value = gv.Rows[i].Cells[j].Text;
                }
            }

            range = ws.Cells[top, left, top + gv.Rows.Count - 1, left + gv.Columns.Count - 1];
            border = range.Style.Border;
            border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;

            top += gv.Rows.Count - 1;
            ws.Cells[rowStartAutofit, left, top, left + gv.Columns.Count - 1].AutoFitColumns();

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;  filename=Ошибки импорта");
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();
        }
    }
}